package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    SpecGroupService specGroupService;
    @Autowired
    SpecParamService specParamService;
    /*
    * 根据分类ID，查询规格组集合
    * */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategory(@RequestParam("id")Long cid) {
        //Wrapper条件，查询全部记录
//        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(specGroupService.querySpecGroupByCategoryId(cid)));

        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(specGroupService.query().eq("category_id",cid).list()));
    }
    /*
    *  新增规格组
    * */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(201).build();
    }
    /*
    *   修改规格组
    * */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /*
    * 新增规格参数
    * */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.save(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(201).build();
    }
    /*
    * 修改规格参数
    * */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(204).build();
    }
    /*
    * 根据条件查询规格参数集合
    * */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamByCategoryIdAndGroupId(
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "groupId",required = false)Long gid,
            @RequestParam(value = "searching",required = false)Boolean search
    ){
        return ResponseEntity.ok(specParamService.querySpecParamByCategoryIdAndGroupId(cid,gid,search));

    }
    /*
    *  根据分类ID查询规格组及组内参数
    * */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id")Long cid) {
        List<SpecGroupDTO> groupDTOList = specGroupService.querySpecGroupDetailByCategoryId(cid);
        return ResponseEntity.ok(groupDTOList);
    }


}
